<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/home/config.php';

$array_return = array();
$array_return['status'] = 0;
$array_return['html'] = '';

$page = getValue('page','int','POST',2);
$start = ($page - 1)*20;
$query = getValue('query','str','POST','');
$query = json_decode(base64_decode($query),1);
//$query = $query[0];
//$array_return['html'] = $query;
//die(json_encode($array_return));

$list_products = db_array('SELECT pro_id,pro_name,pro_alias,pro_image,pro_type,pro_price,pro_instock_status,pro_promotion,
                              pro_display_standard,pro_display_width,pro_general_os,pro_cpuram_core,pro_cpuram_speed,
                              pro_cpuram_ram,pro_cam_primary,pro_connect_duasim,pro_pin_capacity
                           FROM products
                           LEFT JOIN categories ON pro_cat_id = cat_id
                           WHERE pro_active = 1 AND pro_type = 1'.$query.'
                           LIMIT '.$start.',20');
$total_products = count($list_products);
$arr_os = array('android'=>'Android','ios'=>'IOS (iPhone)','windows'=>'Windows Phone','other'=>'HĐH khác');
$list_instock_status = array(0=>'Hết hàng',1=>'Còn hàng',2=>'Sắp về',3=>'Sắp ra mắt');
if($total_products == 0) {
   $array_return['html'] = 'Hết sản phẩm';
}elseif($total_products > 0 && $total_products < 20) {
   foreach($list_products as $product) {
      $array_return['status'] = 2;
      $str_tag1 = $product['pro_promotion'] != '' ? '<span class="tagmotion">Khuyến mãi</span>' : '';
      $str_tag2 = $product['pro_promotion'] != '' ? '<p class="motion">Khuyến mãi lớn</p>' : '';
      $array_return['html'] .= '<div class="proitem">
               '.$str_tag1.'
               <a href="'.url_product_detail($product).'">
                  <img src="'.img_link($product['pro_image'],'small').'" alt="'.$product['pro_name'].'" />
                  <p class="goods">'.$list_instock_status[$product['pro_instock_status']].'</p>
                  <h3 class="name">'.$product['pro_name'].'</h3>
                  <h4 class="price">'.format_price($product['pro_price']).'</h4>
                  '.$str_tag2.'
                  <div class="info">
                     <span>Màn hình: '.$product['pro_display_standard'].', '.$product['pro_display_width'].' inches</span>
                     <span>HĐH: '.$arr_os[$product['pro_general_os']].'</span>
                     <span>CPU: '.$product['pro_cpuram_core'].', '.$product['pro_cpuram_speed'].', RAM '.$product['pro_cpuram_ram'].'</span>
                     <span>Camera: '.$product['pro_cam_primary'].', '.$product['pro_connect_duasim'].'</span>
                     <span>Dung lượng pin: '.$product['pro_pin_capacity'].'</span>
                  </div>
               </a>
            </div>';
   }
}else {
   foreach($list_products as $product) {
      $array_return['status'] = 1;
      $str_tag1 = $product['pro_promotion'] != '' ? '<span class="tagmotion">Khuyến mãi</span>' : '';
      $str_tag2 = $product['pro_promotion'] != '' ? '<p class="motion">Khuyến mãi lớn</p>' : '';
      $array_return['html'] .= '<div class="proitem">
               '.$str_tag1.'
               <a href="'.url_product_detail($product).'">
                  <img src="'.img_link($product['pro_image'],'small').'" alt="'.$product['pro_name'].'" />
                  <p class="goods">'.$list_instock_status[$product['pro_instock_status']].'</p>
                  <h3 class="name">'.$product['pro_name'].'</h3>
                  <h4 class="price">'.format_price($product['pro_price']).'</h4>
                  '.$str_tag2.'
                  <div class="info">
                     <span>Màn hình: '.$product['pro_display_standard'].', '.$product['pro_display_width'].' inches</span>
                     <span>HĐH: '.$arr_os[$product['pro_general_os']].'</span>
                     <span>CPU: '.$product['pro_cpuram_core'].', '.$product['pro_cpuram_speed'].', RAM '.$product['pro_cpuram_ram'].'</span>
                     <span>Camera: '.$product['pro_cam_primary'].', '.$product['pro_connect_duasim'].'</span>
                     <span>Dung lượng pin: '.$product['pro_pin_capacity'].'</span>
                  </div>
               </a>
            </div>';
   }
}

die(json_encode($array_return));
?>